package jscinttools.transmission;

import jscinttools.utilities.aidautilities.AidaFactories;
import hep.aida.ITree;
import hep.aida.ITreeFactory;
import hep.aida.ITuple;
import hep.aida.ITupleFactory;
import java.lang.System;
import java.lang.String;
import java.lang.Double;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import fr.in2p3.ipnl.aidautilities.TupleReader;
import jscinttools.transmission.*;

/**
 *  Change the format for TDC files for Rates calculation
 *
 *@author     Marc-Antoine Verdier
 *@created    25 mai 2011
 */
public class EventLED implements AidaFactories
{
	String path;
	String file;
	String outPath;


	/**
	 *  Constructor for the EventLED object
	 *
	 *@param  path     Description of the Parameter
	 *@param  outPath  Description of the Parameter
	 *@param  file     Description of the Parameter
	 */
	public EventLED(String path, String file, String outPath)
	{
		this.path = path;
		this.file = file;
		this.outPath = outPath;
	}


	/**
	 *  Constructor for the EventLED object
	 *
	 *@param  path  Description of the Parameter
	 *@param  file  Description of the Parameter
	 */
	public EventLED(String path, String file)
	{
		this(path, file, path);
	}


	/**
	 *  Gets the path attribute of the EventLED object
	 *
	 *@return    The path value
	 */
	public String getPath()
	{
		return path;
	}


	/**
	 *  Sets the path attribute of the EventLED object
	 *
	 *@param  path  The new path value
	 */
	public void setPath(String path)
	{
		this.path = path;
	}


	/**
	 *  Gets the file attribute of the EventLED object
	 *
	 *@return    The file value
	 */
	public String getFile()
	{
		return file;
	}


	/**
	 *  Sets the file attribute of the EventLED object
	 *
	 *@param  file  The new file value
	 */
	public void setFile(String file)
	{
		this.file = file;
	}


	/**
	 *  Gets the outPath attribute of the EventLED object
	 *
	 *@return    The outPath value
	 */
	public String getOutPath()
	{
		return outPath;
	}


	/**
	 *  Sets the outPath attribute of the EventLED object
	 *
	 *@param  outPath  The new outPath value
	 */
	public void setOutPath(String outPath)
	{
		this.outPath = outPath;
	}


	/**
	 *  Create an Aida file with a tuple containing the number of stops counted per
	 *  channel for each start. Events (1 start plus N stops) containing errors
	 *  will be skiped.
	 *
	 *@param  treeName  The name of the Aida file
	 */
	public void makeAidaTDC(String treeName)
	{
		try
		{
			try
			{
				ITree tree = AidaFactories.TREE_FACTORY.createTree(path + File.separator + treeName + ".aida", "xml", ITreeFactory.CREATE);
				eventsFromTDC(initTuple(path + File.separator + file), tree);
				tree.commit();
			} catch (FileNotFoundException fnfe)
			{
				System.out.println(fnfe);
			}
		} catch (IOException ioe)
		{
			System.out.println("Problem with aida file: " + ioe);
		}
	}


	/**
	 *  Description of the Method
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@return                            Description of the Return Value
	 *@exception  FileNotFoundException  Description of the Exception
	 */
	private ITuple initTuple(String fullFileName) throws FileNotFoundException
	{
		return new TupleReader(fullFileName).getTuple();
	}


	/**
	 *  Description of the Method
	 *
	 *@param  tuple  Description of the Parameter
	 *@param  tree   Description of the Parameter
	 */
	private void eventsFromTDC(ITuple tuple, ITree tree)
	{
		String tupTit = tuple.title().split("_NoH")[0] + "_events";
		String tupColumns = "int event, int ol, int ch1, int ch2, int ch3, int ch4, int ch5, int ch6, int ch7";
		ITupleFactory tf = AidaFactories.ANALYSIS_FACTORY.createTupleFactory(tree);
		ITuple eventTuple = tf.create(tupTit, tupTit, tupColumns);
		int[] chanPhot = new int[7];
		int ol = 0;
		for (int j = 0; j < chanPhot.length; j++)
		{
			chanPhot[j] = 0;
		}
		tuple.start();
		int i = 0;
		while (tuple.next())
		{
			if ((int) tuple.getDouble(0) == 0)
			{
				if (i > 0)
				{
					eventTuple.fill(1, ol);
					for (int j = 0; j < chanPhot.length; j++)
					{
						eventTuple.fill(j + 2, chanPhot[j]);
					}
					eventTuple.addRow();
				}

				eventTuple.fill(0, (int) tuple.getDouble(2));
				if ((int) tuple.getDouble(1) == 0)
					ol = 0;
				else
					ol = 1;

				for (int j = 0; j < chanPhot.length; j++)
				{
					chanPhot[j] = 0;
				}

			}
			else
			{
				ol += (int) tuple.getDouble(1);
				chanPhot[(int) tuple.getDouble(0) - 1]++;
			}
			i++;
		}

		eventTuple.fill(1, ol);
		for (int j = 0; j < chanPhot.length; j++)
		{
			eventTuple.fill(j + 2, chanPhot[j]);
		}
		eventTuple.addRow();
	}
}

